package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.CustomerRepayPlan;
import org.example.mapper.CustomerRepayPlanMapper;
import org.example.service.CustomerRepayPlanService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

/**
 * 客户还款计划服务实现类
 */
@Service
public class CustomerRepayPlanServiceImpl extends ServiceImpl<CustomerRepayPlanMapper, CustomerRepayPlan> implements CustomerRepayPlanService {
    
    @Override
    public List<CustomerRepayPlan> getByOrderNo(String orderNo) {
        QueryWrapper<CustomerRepayPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        return list(queryWrapper);
    }
    
    @Override
    public CustomerRepayPlan getByBillId(String billId) {
        QueryWrapper<CustomerRepayPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bill_id", billId);
        return getOne(queryWrapper);
    }
    
    @Override
    public List<CustomerRepayPlan> getByRepayStatus(String repayStatus) {
        QueryWrapper<CustomerRepayPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("repay_status", repayStatus);
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerRepayPlan> getByOverdueDaysRange(Integer minDays, Integer maxDays) {
        QueryWrapper<CustomerRepayPlan> queryWrapper = new QueryWrapper<>();
        if (minDays != null) {
            queryWrapper.ge("overdue_days", minDays);
        }
        if (maxDays != null) {
            queryWrapper.le("overdue_days", maxDays);
        }
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerRepayPlan> getByDueDateRange(LocalDate startDate, LocalDate endDate) {
        QueryWrapper<CustomerRepayPlan> queryWrapper = new QueryWrapper<>();
        if (startDate != null) {
            queryWrapper.ge("due_date", startDate);
        }
        if (endDate != null) {
            queryWrapper.le("due_date", endDate);
        }
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerRepayPlan> getByActualRepayDateRange(LocalDate startDate, LocalDate endDate) {
        QueryWrapper<CustomerRepayPlan> queryWrapper = new QueryWrapper<>();
        if (startDate != null) {
            queryWrapper.ge("actual_repay_date", startDate);
        }
        if (endDate != null) {
            queryWrapper.le("actual_repay_date", endDate);
        }
        return list(queryWrapper);
    }
    
    @Override
    public List<CustomerRepayPlan> getOverduePlans() {
        QueryWrapper<CustomerRepayPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("overdue_days", 0);
        return list(queryWrapper);
    }
    
    @Override
    public BigDecimal getOverdueAmount() {
        QueryWrapper<CustomerRepayPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("overdue_days", 0);
        queryWrapper.select("SUM(now_pay_amount) as overdueAmount");
        List<CustomerRepayPlan> list = list(queryWrapper);
        if (list != null && !list.isEmpty()) {
            return list.get(0).getNowPayAmount();
        }
        return BigDecimal.ZERO;
    }
}
